<template>
  <div>
    <div class="n-layout-page-header" style="margin: 0">
      <n-card :bordered="false"
              :header-style="{
                    padding: '20px',
                  }">
        <template #header>
          <text style="font-weight: 500;font-size: 18px;color: #3D3D3D;line-height: 25px;">价格计划</text>
        </template>
        <!--  这是由系统生成的CURD表格，你可以将此行注释改为表格的描述 -->
      </n-card>
    </div>
    <n-card :bordered="false" class="proCard"
            :content-style="{
                    padding: '0 20px 20px',
                  }">
      <BasicForm  ref="searchFormRef" @register="register" @submit="reloadTable" @reset="reloadTable" @keyup.enter="reloadTable">
        <template #statusSlot="{ model, field }">
          <n-input v-model:value="model[field]" />
        </template>
      </BasicForm>
      <BasicTable  ref="actionRef" :openChecked="false" :columns="columns" :request="loadDataTable" :actionColumn="actionColumn" :scroll-x="scrollX" :resizeHeightOffset="-10000">
        <template #tableTitle>
          <n-button type="primary"  @click="addTable" class="min-left-space" v-if="hasPermission(['/pricePlan/edit'])">
            <template #icon>
              <n-icon>
                <PlusOutlined />
              </n-icon>
            </template>
            新增计划
          </n-button>
        </template>
      </BasicTable>
    </n-card>
    <Edit ref="editRef" @reloadTable="reloadTable" />
    <View ref="viewRef" />
  </div>
</template>

<script lang="ts" setup>
import { h, reactive, ref, computed, onMounted } from 'vue';
import { useDialog, useMessage } from 'naive-ui';
import { BasicTable, TableAction } from '@/components/Table';
import { BasicForm, useForm } from '@/components/Form/index';
import { usePermission } from '@/hooks/web/usePermission';
import { List, Status } from '@/api/pmsPricePlan';
import { PlusOutlined } from '@vicons/antd';
import { columns, schemas, getRoomTypeList } from './model';
import { adaTableScrollX } from '@/utils/hotgo';
import Edit from "@/views/pmsPricePlan/edit.vue";
import View from "@/views/pmsPricePlan/view.vue";
import {useRouter} from "vue-router";
import {useUserStore} from "@/store/modules/user";

const router = useRouter();
const dialog = useDialog();
const message = useMessage();
const { hasPermission } = usePermission();
const actionRef = ref();
const searchFormRef = ref<any>({});
const editRef = ref();
const viewRef = ref();
const userStore = useUserStore();

const actionColumn = reactive({
  width: 288,
  title: '操作',
  key: 'action',
  fixed: 'right',
  render(record) {
    return h(TableAction as any, {
      style: 'button',
      actions: [
        {
          label: '详情',
          onClick: handleView.bind(null, record),
          auth: ['/pricePlan/view'],
        },
        {
          label: '编辑',
          onClick: handleEdit.bind(null, record),
          auth: ['/pricePlan/edit'],
        },
        {
          label: '禁用',
          onClick: handleStatus.bind(null, record, "N"),
          ifShow: () => {
            return record.pricePlanStatus === "Y";
          },
          auth: ['/pricePlan/status'],
        },
        {
          label: '启用',
          onClick: handleStatus.bind(null, record, "Y"),
          ifShow: () => {
            return record.pricePlanStatus === "N";
          },
          auth: ['/pricePlan/status'],
        },
      ],
    });
  },
});

const scrollX = computed(() => {
  return adaTableScrollX(columns, actionColumn.width);
});

const [register, {}] = useForm({
  gridProps: { cols: '1 s:1 m:2 l:3 xl:4 2xl:4' },
  labelWidth: 120,
  schemas,
});

// 加载表格数据
const loadDataTable = async (res) => {
  return await List({ ...searchFormRef.value?.formModel, ...res });
};

// 重新加载表格数据
function reloadTable() {
  actionRef.value?.reload();
}

// 添加数据
function addTable() {
  if (router.currentRoute.value.query?.uid) {
    var puid = router.currentRoute.value.query.uid;
  }else{
    var puid = userStore.getuserPms.uid;
  }
  editRef.value.openModal({propertyId: puid});
}

// 查看详情
function handleView(record: Recordable) {
  viewRef.value.openModal(record);
}

// 编辑数据
function handleEdit(record: Recordable) {
  if (router.currentRoute.value.query?.uid) {
    record.propertyId = router.currentRoute.value.query.uid;
  }else{
    record.propertyId = userStore.getuserPms.uid;
  }
  editRef.value.openModal(record);
}

// 修改状态
function handleStatus(record: Recordable, status: string) {
  Status({ id: record.id, status: status }).then((_res) => {
    message.success('修改成功');
    setTimeout(() => {
      reloadTable();
    });
  });
}

onMounted(() => {
  if (router.currentRoute.value.query?.uid) {
    var propertyId = router.currentRoute.value.query.uid;
  }else{
    var propertyId = userStore.getuserPms.uid;
  }
  getRoomTypeList(propertyId);
});
</script>

<style lang="less" scoped></style>
